﻿/**
 * Vector2
 * 
 * @author		Allen Chou
 * @version		1.0.5 (last update: Nov 25 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

package idv.cjcat.geom {
	import flash.errors.IllegalOperationError;

	/**
	 * The <code>CJGeom</code> class contains several geometric utility methods.
	 */
	public class CJGeom {
		
		//2D geometry
		//------------------------------------------------------------------------------------------------
		
		/**
		 * Checks if an object has defined <code>x</code> and <code>y</code> properties.
		 * @param	obj  The object.
		 * @return If the object has defined <code>x</code> and <code>y</code> properties.
		 */
		public static function isVec2D(obj:Object):Boolean {
			if (obj.x == undefined  || obj.y == undefined) return false;
			return true;
		}
		
		/**
		 * Computes the distance between two objects in 2D.
		 * Note that both objects must have <code>x</code>, and <code>y</code> properties defined.
		 * @param	obj1  The first object.
		 * @param	obj2  The second object.
		 * @return The distance.
		 */
		public static function distance(obj1:Object, obj2:Object):Number {
			return Math.sqrt(Math.pow(obj1.x - obj2.x, 2) + Math.pow(obj1.y - obj2.y, 2));
		}
		
		//------------------------------------------------------------------------------------------------
		//end of 2D geometry
		
		
		//3D geometry
		//------------------------------------------------------------------------------------------------
		
		/**
		 * Checks if an object has defined <code>x</code>, <code>y</code>, and <code>z</code> properties.
		 * @param	obj  The object.
		 * @return If the object has defined <code>x</code>, <code>y</code>, and <code>z</code> properties.
		 */
		public static function isVec3D(obj:Object):Boolean {
			if (obj.x == undefined || obj.y == undefined || obj.z == undefined) return false;
			return true;
		}
		
		/**
		 * Computes the distance between two objects in 2D.
		 * Note that both objects must have <code>x</code>, <code>y</code>, and <code>z</code> properties defined.
		 * @param	obj1  The first object.
		 * @param	obj2  The second object.
		 * @return The distance.
		 */
		public static function distance3D(obj1:Object, obj2:Object):Number {
			return Math.sqrt(Math.pow(obj1.x - obj2.x, 2) + Math.pow(obj1.y - obj2.y, 2) + Math.pow(obj1.z - obj2.z, 2));
		}
		
		//------------------------------------------------------------------------------------------------
		//end of 3D geometry
	}
}